#include<pRecorder.h>
PRecorder::PRecorder(std::string path):Recorder(path){}
PRecorder::~PRecorder(){}

bool PRecorder::write(std::string name1, std::string name2, std::string key1, std::string val1, std::string key2, std::string val2, std::string key3, std::string val3){
    int pid = getpid();
    //std::cerr<<"pzbsuo\n";
    sem_b_down(pid);//b--
    std::cerr<<"write\n";
    //std::cerr<<"b--";
    std::string filename = path+"/middle"+std::to_string(pid)+".txt";//p要打开的文件名
    std::fstream outfile(filename,std::ios::out | std::ios::app);  //打开文件
    if (!outfile.is_open()) {          // 检查是否成功打开
        std::cout << "Failed to open file.\n" << std::endl;
        return false;
    }
    //要写入中间文件的字符串
    std::string temp = slice1+name1+slice2+name2+slice3+key1+slice4+val1+slice5+key2+slice6+val2+slice7+key3+slice8+val3+"\n";//slice9+slice10+slice11;
    outfile<<temp;
    outfile.close();

    sem_a_up(pid);//a++
    //std::cerr<<"pjiesuo\n";

    return true;
}
void PRecorder::writeEnd(){
    int pid = getpid();
    //std::cerr<<"pzbsuo\n";
    sem_b_down(pid);//b--
    std::cerr<<"write\n";
    //std::cerr<<"b--";
    std::string filename = path+"/middle"+std::to_string(pid)+".txt";//p要打开的文件名
    std::fstream outfile(filename,std::ios::out | std::ios::app);  //打开文件
    if (!outfile.is_open()) {          // 检查是否成功打开
        std::cout << "Failed to open file.\n" << std::endl;
        return;
    }
    //要写入中间文件的字符串
    std::string temp = "end";
    outfile<<temp;
    outfile.close();

    sem_a_up(pid);//a++
    sleep(1);
    //std::cerr<<"pjiesuo\n";

}



